#include <stdio.h>
#include <Length.h>

static const LENGTH_UNIT lengthUnit[] = {MILE, YARD, FEET, INCH};
static const char* lengthUnitString[] = {"MILE", "YARD", "FEET", "INCH"};
static const unsigned int lengthUnitSorts = sizeof(lengthUnit) / sizeof(LENGTH_UNIT);

Length::Length(unsigned int amount, LENGTH_UNIT unit) : Measurement<LENGTH_UNIT>(amount, unit)
{}

void Length::formatInWhole(char* str)
{
    unsigned unitProportion;
    unsigned int quotient;
    unsigned int residue = amount_m;
    unsigned int index = 0;

    for(unsigned int i = 0; i < lengthUnitSorts; i++)
    {
        unitProportion = lengthUnit[i] / unit_m;
        quotient = residue / unitProportion;
        residue %= unitProportion;
        if(quotient != 0)
        {
            index += sprintf(&str[index], "%d %s", quotient, lengthUnitString[i]);
            if(residue != 0) 
                index += sprintf(&str[index], " ");
            else
                break;
        }
    }

    str[index]='\0';
}

void Length::formatInInch(char* str)
{
    amount_m *= unit_m / INCH;

    sprintf(str, "%d INCH", amount_m);
}
